package com.camapp.file2image.widget;

import com.camapp.file2image.activity.R;
import com.camapp.file2image.model.ClipPoint;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.FillType;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class SelectAndClipWidget extends View {
	
	private Bitmap image;
	private int height, width;
	private ClipPoint[] points;
	private Paint mPaint;
	private ClipPoint selectedPoint;
	public SelectAndClipWidget(Context context) {
		this(context, null, 0);
	}
	
	public SelectAndClipWidget(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public SelectAndClipWidget(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		image = BitmapFactory.decodeResource(getResources(), R.drawable.test);
		points = new ClipPoint[] {
			new ClipPoint(), new ClipPoint(), 
			new ClipPoint(), new ClipPoint()};
		mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		height = getHeight();
		width = getWidth();
		System.out.println("width:" + getWidth());
		System.out.println("height:" + getHeight());
		
		int rectWidth = 200;
		int rectHeight = 320;
		
		float x1 = width / 2 - rectWidth / 2;
		float y1 = height / 2 - rectHeight / 2;
		
		float x2 = x1 + rectWidth;
		float y2 = y1;
		
		float x3 = x2;
		float y3 = y2 + rectHeight;
		
		float x4 = x1;
		float y4 = y1 + rectHeight;
		
		points[0].setX(x1);
		points[0].setY(y1);
		points[1].setX(x2);
		points[1].setY(y2);
		points[2].setX(x3);
		points[2].setY(y3);
		points[3].setX(x4);
		points[3].setY(y4);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawBitmap(image, 0, 0, null);
		Paint paint = new Paint();
		Path path = new Path();
		path.setFillType(FillType.INVERSE_WINDING);
		path.moveTo(points[0].getX(), points[0].getY());
		for (int i = 1; i < points.length; i++) {
			path.lineTo(points[i].getX(), points[i].getY());
		}
		paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setColor(Color.GRAY);
		paint.setAlpha(150);
		canvas.drawPath(path, paint);
		
		paint = mPaint;
		paint.setColor(Color.RED);
		for(ClipPoint point : points) {
			canvas.drawCircle(point.getX(), point.getY(), ClipPoint.radius, paint);
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch(event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				selectedPoint = getPoint(event.getX(), event.getY());
				break;
			case MotionEvent.ACTION_MOVE:
				if (selectedPoint != null) {
					selectedPoint.setX(event.getX());
					selectedPoint.setY(event.getY());
					invalidate();
				}
				break;
			case MotionEvent.ACTION_UP:
//				selectedPoint = null;
				break;
		}
		return true;
	}
	
	private ClipPoint getPoint(float x, float y) {
		for (ClipPoint point : points) {
			if (point.hint(x, y)) {
				return point;
			}
		}
		return null;
	}

}
